//
//  WebView.m
//  RootNotes
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 大远. All rights reserved.
//

#import "WebView.h"

@interface WebView ()<UIWebViewDelegate>
@property(nonatomic, retain)UIWebView *webView;
@property (nonatomic, strong)CoreDataManager *manager;

@end
@implementation WebView{
    UIButton *_Collerbutton;
    BOOL _bollcollect;
    NSArray *_collarray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.webView = [[UIWebView alloc] init];
    [self.view addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(SV).with.insets(UIEdgeInsetsMake(-100, 0, -100, 0));
    }];
    self.view.backgroundColor = [UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1];
    self.webView.delegate  = self;
    self.webView.scalesPageToFit = YES;
    self.webView.scrollView.bounces = NO;
    self.navigationItem.title = self.tittle;
    
    if (!self.html) {

    // 请求地址
    NSURL *url = [NSURL URLWithString:self.url];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [self.webView loadRequest:request];
    }else{
        [self.webView loadHTMLString:self.HtmlStr baseURL:nil];
    }
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"fenxiang"] style:UIBarButtonItemStylePlain target:self action:@selector(share)];
    [self.navigationItem.rightBarButtonItem setTintColor:BUTTONCOLOR];
    _Collerbutton = [UIButton buttonWithType:UIButtonTypeSystem];
    _Collerbutton.frame = CGRectMake(WIDTH - 70, 10, 24, 24);
    [self.navigationController.navigationBar.viewForBaselineLayout addSubview:_Collerbutton];
    self.manager = [CoreDataManager defaults];
    
    
    _bollcollect = [self lookcollect];
    
    if (_bollcollect) {
    [_Collerbutton setImage:[UIImage imageNamed:@"ysc"] forState:UIControlStateNormal];
    [_Collerbutton setTintColor:[UIColor redColor]];
    }else{
    [_Collerbutton setImage:[UIImage imageNamed:@"wsc"] forState:UIControlStateNormal];
    [_Collerbutton setTintColor:BUTTONCOLOR];
    }
    
    
    [_Collerbutton addTarget:self action:@selector(collect:) forControlEvents:UIControlEventTouchUpInside];
    /**
     *  查询是否收藏过
     */
}
- (void)collect:(UIBarButtonItem *)buttonItem{
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Collect" inManagedObjectContext:self.manager.managedObjectContext];
    
    if (_bollcollect) {
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        [fetchRequest setEntity:entity];

        NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@",@"title",@"%@"],self.tittle];
        [fetchRequest setPredicate:predicate];
        NSError *error = nil;
        NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
        for (Collect *temp in fetchedObjects) {
            [self.manager.managedObjectContext deleteObject:temp];
        }
        [self.manager saveContext];
        [_Collerbutton setImage:[UIImage imageNamed:@"wsc"] forState:UIControlStateNormal];
        [_Collerbutton setTintColor:BUTTONCOLOR];
        _bollcollect = NO;
    }else{
        Collect *ect = [[Collect alloc]initWithEntity:entity insertIntoManagedObjectContext:self.manager.managedObjectContext];
        ect.type = @"web";
        ect.code = self.url;
        ect.title = self.tittle;
        ect.number =@0;
        [self.manager saveContext];
        [_Collerbutton setImage:[UIImage imageNamed:@"ysc"] forState:UIControlStateNormal];
        [_Collerbutton setTintColor:[UIColor redColor]];
        _bollcollect = YES;
    }
    
    
}
- (BOOL)lookcollect{
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Collect"];
    
    //添加约束  谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@",@"title",@"%@"],self.tittle];
    request.predicate = predicate;

    NSError *error;
    _collarray = [[self.manager managedObjectContext] executeFetchRequest:request error:&error];
    
    if (_collarray.count > 0) {
        return YES;
    }
    
    return NO;
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
    
    // 进度轮开始
    [ShowMytext showHUDloadView:SV];
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(webViewDidFinishLoad:) userInfo:nil repeats:NO];
}
- (void)share{
    shareModel *model = [[shareModel alloc]init];
    model.code = self.url;
    [shareModel sharedInstance].title = self.tittle;
    [shareModel sharedInstance].type = Look;
    [shareModel sharedInstance].code = self.url;
    [[ShareWindow sharedInstance] show];
}
- (void)viewDidDisappear:(BOOL)animated

{
    NSLog(@"webView退出了");
    [super viewDidDisappear:animated];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
    [_Collerbutton removeFromSuperview];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    // 进度轮结束
    [ShowMytext hiddHUDloadView];
    
}
- (IBAction)presentLeftMenuViewController:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
    [[webWindow sharedInstance] hidd];
    
}
@end
